package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("车辆查询业务：{}", vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        return list;
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        log.debug("车辆保存业务：{}", vehicleSaveParam);
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam, vehicle);
        //判断车辆是否已存在
        VehicleVO vehicleVO = vehicleMapper.selectByLicense(vehicle.getLicense());
        if(vehicle.getId()==null){
            if(vehicleVO!=null){
                throw new ServiceException(StatusCode.VEHICLE_EXISTS);
            }
            vehicle.setStatus("1");
            vehicle.setCreateTime(new Date());
            vehicle.setGeofenceBindStatus("0");
            vehicleMapper.insert(vehicle);
        }else {
            //如果车牌号已存在，且当前修改的id与查出的id不一致，则抛出异常（不能将车牌号改为别人已存在的车牌号）
            if(vehicleVO != null && !vehicleVO.getId().equals(vehicle.getId())){
                throw new ServiceException(StatusCode.LICENSE_EXISTS);
            }
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void deleteVehicle(Long id) {
        log.debug("车辆删除业务：{}", id);
        vehicleMapper.deleteVehicle(id);
    }

    @Override
    public void unbindVehicle(Long vehicleId) {
        log.debug("解绑车辆业务参数：{}", vehicleId);
        //创建空对象用来封装更新条件
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);//设置修改哪辆车
        vehicle.setGeofenceBindStatus("0");//设置围栏绑定状态为"未绑定"
        vehicle.setGeofenceId(null);//设置绑定的围栏id为null
        vehicle.setUpdateTime(new Date());
        /* 之前更新的SQL要求围栏id不为null才能拼接更新条件,但此处业务需要将围栏id设置为null值
         * 所以需要新写一个Mapper方法和对应的SQL*/
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bindVehicle(Long geofenceId, Long vehicleId) {
        log.debug("车辆绑定业务：geofenceId{}, vehicleId{}", geofenceId, vehicleId);
        Vehicle vehicle=new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geofenceId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
